﻿using Newtonsoft.Json;
using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.com.Attbutes;
using QG.FrontDesk.com.ChatHubs;
using QG.FrontDesk.com.Models;
using QG.FrontDesk.Core;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace QG.FrontDesk.com.Controllers
{
    public class ResumeController : Controller
    {
        private IResumesByCompanyService _resumesByCompanyService;

        private ICompanyService _companyService;
        

        public ResumeController(IResumesByCompanyService resumesByCompanyService,ICompanyService companyService)
        {
            DataContext<Company>.context.model = null;
            DataContext<UserInfo>.context.model = null;
            //获取存在的cookie
            //再次浏览器判断是否有值
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (cookie != null)
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (ticket.Name == "求职")
                {
                    DataContext<UserInfo>.context.model = JsonConvert.DeserializeObject<UserInfo>(userData);
                }
                else if (ticket.Name == "公司")
                {
                    DataContext<Company>.context.model = JsonConvert.DeserializeObject<Company>(userData);
                }
            }
            _resumesByCompanyService = resumesByCompanyService;
            _companyService = companyService;
        }

        ResponseResult result = new ResponseResult();

        /// <summary>
        /// 自动过滤简历页面
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult AutoFilterResumes(int page=1)
        {
            int CompId = DataContext<Company>.context.model.CompId;
            List<Position_ResumeInfo> resumes = _resumesByCompanyService.GetResumeInfos(CompId, 5);
            ViewBag.resumes = resumes;
            ViewBag.Count = resumes.Count % 3 > 0 ? resumes.Count / 3 + 1 : resumes.Count / 3;//总页数
            ViewBag.positions = resumes.Skip((page - 1) * 3).Take(3).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            return View();
        }
        /// <summary>
        /// 待定简历
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult CanInterviewResumes(int page = 1)
        {
            int CompId = DataContext<Company>.context.model.CompId;
            List<Position_ResumeInfo> resumes = _resumesByCompanyService.GetResumeInfos(CompId, 2);
            ViewBag.resumes = resumes;
            ViewBag.Count = resumes.Count % 3 > 0 ? resumes.Count / 3 + 1 : resumes.Count / 3;//总页数
            ViewBag.positions = resumes.Skip((page - 1) * 3).Take(3).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            return View();
        }
        /// <summary>
        /// 不合适简历 
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult HaveRefuseResumes(int page = 1)
        {
            int CompId = DataContext<Company>.context.model.CompId;
            List<Position_ResumeInfo> resumes = _resumesByCompanyService.GetResumeInfos(CompId, 4);
            ViewBag.resumes = resumes;
            ViewBag.Count = resumes.Count % 3 > 0 ? resumes.Count / 3 + 1 : resumes.Count / 3;//总页数
            ViewBag.positions = resumes.Skip((page - 1) * 3).Take(3).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            return View();
        }
        /// <summary>
        /// 已通知面试简历
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult HaveNoticeResumes(int page = 1)
        {
            int CompId = DataContext<Company>.context.model.CompId;
            List<Position_ResumeInfo> resumes = _resumesByCompanyService.GetResumeInfos(CompId, 3);
            ViewBag.resumes = resumes;
            ViewBag.Count = resumes.Count % 3 > 0 ? resumes.Count / 3 + 1 : resumes.Count / 3;//总页数
            ViewBag.positions = resumes.Skip((page - 1) * 3).Take(3).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            return View();
        }
        /// <summary>
        /// 待处理简历
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult PendingResumes(int page = 1)
        {
            int CompId = DataContext<Company>.context.model.CompId;
            List<Position_ResumeInfo> resumes = _resumesByCompanyService.GetResumeInfos(CompId, 1);
            ViewBag.resumes = resumes;
            ViewBag.Count = resumes.Count % 10 > 0 ? resumes.Count / 10 + 1 : resumes.Count / 10;//总页数
            ViewBag.positions = resumes.Skip((page - 1) * 3).Take(3).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            return View();
        }

        /// <summary>
        /// 修改简历状态
        /// </summary>
        /// <param name="PRId"></param>
        /// <param name="statu"></param>
        /// <returns></returns>
        public JsonResult UpdateResumeStatu(int PRId,short statu) {
            int CompId = DataContext<Company>.context.model.CompId;
            result.success = _resumesByCompanyService.UpdateResumeStatu(PRId,statu)>0;
            result.data = _companyService.GetCompanyInfo(CompId);
            return Json(result);
        }
        /// <summary>
        /// 发送通知面试邮件
        /// </summary>
        /// <returns></returns>
        public JsonResult SendNoticeEmail(string name,string email,string subject,DateTime interTime,string interAdd,
            string linkMan,string linkPhone,string content,int PRId,string UserId) {

            string text = name + "，您好：<br />您的简历已通过我们的筛选，很高兴通知您参加我们的面试。<br /><br />面试时间：" + interTime + "<br />面试地点：" + interAdd + "<br />";
            if (!string.IsNullOrEmpty(linkMan))
            {
                text += "联系人：" + linkMan + "<br />";
            }
            text += "联系电话：" + linkPhone + "<br /><br />"+content;
                       
            //string companyEmail = DataContext<Company>.context.model.CompanyEmail;
            result.success = true;//EmailHelper.SendEmail(email, subject, text);
            return Json(result);
        }
        /// <summary>
        /// 发送不合适邮箱
        /// </summary>
        /// <param name="email"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public JsonResult SendRefuseEmail(string email,string content) {                      
            string companyEmail = DataContext<Company>.context.model.CompanyEmail;
            result.success = EmailHelper.SendEmail(email, "简历", content);
            return Json(result);
        }
        /// <summary>
        /// 发送转发邮件
        /// </summary>
        /// <returns></returns>
        public JsonResult SendForwardEmail(string recipients,string title,string content,int deliverId) {           
            string companyEmail = DataContext<Company>.context.model.CompanyEmail;
            result.success = EmailHelper.SendEmail(recipients, title, content);
            return Json(result);
        }

        /// <summary>
        /// 删除简历
        /// </summary>
        /// <param name="PRId"></param>
        /// <returns></returns>
        public JsonResult DeleteResume(int PRId) {
            result.success = _resumesByCompanyService.DeleteResume(PRId)>0;
            return Json(result);
        }
        /// <summary>
        /// 修改为已查看
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdateLookStatu(int PRId) {
            result.success = _resumesByCompanyService.UpdateLookStatu(PRId) > 0;
            return Json(result);
        }

       
    }
}